
package com.hx.zhg.web.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 非法访问过滤器
 */
public class InvalidRequestFilter extends BaseFilter {

    private static final Logger log = LoggerFactory.getLogger(InvalidRequestFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        String requestURI = req.getRequestURI();
        //直接访问跟目录让其跳转倒ping
        if ("/".equalsIgnoreCase(requestURI)) {
            // 设置为301跳转
            res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            res.sendRedirect("/swagger-ui.html");
            return;
        }
        boolean isStaticFile = super.isStaticFile(requestURI, staticSuffix);
        if (isStaticFile || requestURI.equals("/ping")) {
            chain.doFilter(req, res);
            return;
        }
        chain.doFilter(req, res);
    }

}
